package tg.app.rj.tslx;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSet;
import psdi.mbo.MboSetRemote;
import psdi.mbo.StatefulMbo;
import psdi.mbo.StatusHandler;
import psdi.security.UserInfo;
import psdi.server.MXServer;
import psdi.util.MXApplicationException;
import psdi.util.MXException;

/****
 * 
 * @author pzx
 * 
 */
public class TslxMbo extends StatefulMbo
  implements TslxMboRemote
{
  String personId = getUserInfo().getPersonId();
  String[] string1 = { "DESCRIPTION", "ASSETNUM", "ZY", "FWKZS" };
  String[] string2 = { "LDHGP", "LZGLWZ", "LZJXWZ", "TDSJ", "TDYY" };
  String[] string3 = { "LDHGP1", "LZGLWZ1", "LZJXWZ1", "CZJSSJ1" };
  String[] string4 = { "JHR" };
  String[] string5 = { "CJYSD", "SZSYWZ", "SZGZWZ", "SDSJ", "SDYY" };
  String[] string6 = { "CJYSD1", "SZSYWZ1", "SZGZWZ1", "CZJSSJ2" };
  String[] string7 = { "JHR1" };

  public TslxMbo(MboSet ms)
    throws RemoteException
  {
    super(ms);
  }

  public void init()
    throws MXException
  {
    try
    {
      if (!isNew())
      {
    	/*
        String user = getString("CREATEBY");
        String status = getString("STATUS");
        
       // String nextRole = getString("WFSTATUS.TITLE");
        MboSetRemote adminSet = getMboSet("$groupuser", "GROUPUSER", "groupname in('MAXADMIN') and userid= '" + this.personId +  "'");
        if (!adminSet.isEmpty()) {
          return;
        }
        if ("WAPPR".equalsIgnoreCase(status)){
          if (user.equalsIgnoreCase(this.personId)) {
            setFieldFlag(this.string1, 7L, false);
            setFieldFlag(this.string2, 7L, false);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, false);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          } else {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, true);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          }
        }

        //boolean flag = isWFActive();
        if (("CLOSE".equalsIgnoreCase(status)) || 
          ("CAN".equalsIgnoreCase(status)))
        {
          setFlag(7L, true);
          return;
        }
        */
        /*
        if (flag) {
          List list = getWfstatus();
          if (!list.contains(this.personId)) {
            setFlag(7L, true);
            return;
          }

          if ((nextRole.equalsIgnoreCase("停电申请批准")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, false);
            setFieldFlag("TDSJ", 128L, true);
            setFieldFlag("TDYY", 128L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, true);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          } else if ((nextRole.equalsIgnoreCase("停电执行批准")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, true);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          } else if ((nextRole.equalsIgnoreCase("停电操作")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, false);
            setFieldFlag("CZJSSJ1", 128L, true);
            setFieldFlag(this.string4, 7L, false);
            setFieldFlag(this.string4, 128L, true);
            setFieldFlag(this.string5, 7L, true);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          } else if ((nextRole.equalsIgnoreCase("申请人确认停电")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, false);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
            setFieldFlag("SDSJ", 7L, false);
            setFieldFlag("SDYY", 7L, false);
          } else if ((nextRole.equalsIgnoreCase("送电申请批准")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, false);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          }
          else if ((nextRole.equalsIgnoreCase("送电执行批准")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, true);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          }
          else if ((nextRole.equalsIgnoreCase("送电操作")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, true);
            setFieldFlag(this.string6, 7L, false);
            setFieldFlag(this.string7, 7L, false);
            setFieldFlag(this.string7, 128L, true);
            setFieldFlag("CZJSSJ2", 128L, true);
          }
          else if ((nextRole.equalsIgnoreCase("重新填写")) && 
            (list.contains(this.personId))) {
            setFieldFlag(this.string1, 7L, false);
            setFieldFlag(this.string2, 7L, false);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, false);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          }
          else {
            setFieldFlag(this.string1, 7L, true);
            setFieldFlag(this.string2, 7L, true);
            setFieldFlag(this.string3, 7L, true);
            setFieldFlag(this.string4, 7L, true);
            setFieldFlag(this.string5, 7L, true);
            setFieldFlag(this.string6, 7L, true);
            setFieldFlag(this.string7, 7L, true);
          }
        }
        */
        super.init();
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public void add() throws MXException, RemoteException {
    super.add();
    setFieldFlag(this.string3, 7L, true);
    setFieldFlag(this.string4, 7L, true);
    setFieldFlag(this.string6, 7L, true);
    setFieldFlag(this.string7, 7L, true);
    Date nowDate = MXServer.getMXServer().getDate();
    setValue("STATUS", "WAPPR", 11L);
    setValue("STATUSDATE", nowDate, 11L);
    setValue("CREATEBY", this.personId, 11L);
    setValue("SQBM", getString("CREATEBY.DEPARTMENT"));
    setValue("CREATEDATE", nowDate, 11L);
    setValue("siteid", getInsertSite(), 11L);
    setValue("orgid",getInsertOrganization(), 11L);
  }

  public void modify() throws MXException, RemoteException {
    setValue("changedate", MXServer.getMXServer().getDate());
    setValue("changeby", getUserInfo().getPersonId());
    super.modify();
  }

  public void canDelete() throws MXException, RemoteException {
    super.canDelete();
    String wfactive = getString("WFACTIVE");
    if ((!"N".equalsIgnoreCase(wfactive)) || (!"WAPPR".equalsIgnoreCase(getString("STATUS")))){
      throw new MXApplicationException("sbxxgl", "cannotdelete");
    }
  }

  public void save() throws MXException, RemoteException
  {
    super.save();
  }

  protected MboSetRemote getStatusHistory() throws MXException, RemoteException
  {
    return getMboSet("TSLXSTATUS");
  }

  protected StatusHandler getStatusHandler() {
    return new TslxStatusHandler(this);
  }

  public String getStatusListName() {
    return "TSLXSTATUS";
  }

  private boolean isWFActive()
  {
    try
    {
      long id = getLong("T_ZC_TSDLXID");
      MboSetRemote msr_instance = getMboSet("$instance", "WFINSTANCE", "ownertable='T_ZC_TSDLX' and ownerid='" + id + "' and active = 1");

      if (!msr_instance.isEmpty()){
        setFieldFlag("WFACTIVE", 7L, false);
        setValue("WFACTIVE", "1");
        setFieldFlag("WFACTIVE", 7L, true);
        return true;
      }
      setFieldFlag("WFACTIVE", 7L, false);
      setValue("WFACTIVE", "0");
      setFieldFlag("WFACTIVE", 7L, true);
      return false;
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  private List<String> getWfstatus()
    throws RemoteException, MXException
  {
    String roleid = "";
    List list = new ArrayList();
    MboSetRemote wfstatusset = getMboSet("WFSTATUS");
    for (int i = 0; i < wfstatusset.count(); i++) {
      roleid = wfstatusset.getMbo(i).getString("ASSIGNCODE");
      list.add(roleid);
    }
    return list;
  }

  public String getAttributeName() throws MXException, RemoteException {
    return "TSDLXNUM";
  }
}